package com.dq.redis.redisson;

import com.dq.redis.properties.RedisPropertiesConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.redisson.Redisson;
import org.redisson.config.Config;

/**
 * redisson 配置类
 *
 * @author 凡
 */
@Configuration
public class RedissonConfig {
    @Autowired
    private RedisPropertiesConfig redisPropertiesConfig;

    @Bean
    public Redisson redisson() {
        Config config = new Config();
        if(StringUtils.isNotBlank(redisPropertiesConfig.getPassword())){
            config.useSingleServer()
                    .setAddress("redis://" + redisPropertiesConfig.getHost() + ":"+ redisPropertiesConfig.getPort())
                    .setTimeout(Integer.parseInt(redisPropertiesConfig.getTimeout()))
                    .setPassword(redisPropertiesConfig.getPassword());
        }else{
            config.useSingleServer()
                    .setAddress("redis://" + redisPropertiesConfig.getHost() + ":"+ redisPropertiesConfig.getPort())
                    .setTimeout(Integer.parseInt(redisPropertiesConfig.getTimeout()));
        }

        // Redisson类 实现了 RedissonClient类
        return (Redisson) Redisson.create(config);
    }

}